package com.study.java._02anno_reflec.type;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * <pre>
 *     author : slx
 *     time   : 2020/12/01
 *     desc   :
 * </pre>
 */
public class TestParameterizedType {
    public Map<String, String> map;

    public static void main(String[] args) {
        try {
            Field map = TestParameterizedType.class.getDeclaredField("map");
            System.out.println(map.getGenericType());  // java.util.Map<java.lang.String, java.lang.String>
            ParameterizedType parameterizedType = (ParameterizedType) map.getGenericType();

            // 返回承载该泛型信息的对象, 如上面那个Map<String, String>承载范型信息的对象是Map
            System.out.println(parameterizedType.getRawType()); // interface java.util.Map

            //返回实际泛型类型列表, 如上面那个Map<String, String>实际范型列表中有两个元素, 都是String
            for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
                System.out.println(actualTypeArgument);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}